package com.xcity.util;

public class ExpCalc2 {

	private int[] exps; // 每级所需经验（非累计），从一级开始
	
	public ExpCalc2(int[] exps) {
		this.exps = exps;
	}

	public int getMaxLevel() {
		return exps.length;
	}
	
	/**
	 * 获取该等级所需经验（非累计）
	 * @param level
	 * @return
	 */
	public int getExp(int level) {
		int i = level - 1;
		if (i < 0 || i >= exps.length) {
			i = exps.length - 1;
		}
		return exps[i];
	}
	
	/**
	 * 计算经验值expAdded可升到多少级
	 * @param level
	 * @param expAdded
	 * @return int[] { 最终等级, 剩余经验 }
	 */
	public int[] calc(int level, int expAdded) {
		for (int i = level; i < exps.length; i++) {
			int exp = exps[i];
			if (expAdded < exp) {
				break;
			}
			expAdded -= exp;
			++level;
		}
		return new int[] { level, expAdded };
	}
	
}
